package com.hyk.learning.registry.zk;

import com.hyk.learning.registry.ServiceRegistry;
import com.hyk.learning.registry.zk.util.CuratorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;

import java.net.InetSocketAddress;

/**
 * @className: ZkServiceRegistryImpl
 * @author: hyk199710
 * @description: 服务注册（基于zookeeper实现）
 * @date: 2024/5/10 22:11
 */
@Slf4j
public class ZkServiceRegistryImpl implements ServiceRegistry {

    private final String RETRY = "canRetry";
    @Override
    public void registerService(String rpcServiceName, InetSocketAddress inetSocketAddress, boolean canRetry) {
        String servicePath = CuratorUtils.ZK_REGISTER_ROOT_PATH + "/" + rpcServiceName + inetSocketAddress.toString();
        if(canRetry){
            servicePath = CuratorUtils.ZK_REGISTER_ROOT_PATH + "/" + RETRY + "/" +rpcServiceName + inetSocketAddress.toString();
        }

        CuratorFramework zkClient = CuratorUtils.getZkClient();
        CuratorUtils.createPersistentNode(zkClient, servicePath);
    }
}
